console.log($event);
const { type, params, instanceName } = $event;
const dev = this.pageObj.getVariables().find(v => v.name === 'environmentType')?.value || '';
const gis = this.getApiPage().getApiWidgetByAliasName('三维组件');
const queryManage = this.pageObj.getApiWidgetByAliasName('queryManage')?.widgetObj;
const publicParams = this.getApiPage().getVariables().find(v => v.name === 'panel_type');
const search = this.getApiPage().getVariables().find(v => v.name === 'event_search');

function basicsFetch(url) {
  const headers = { "Content-type": "application/json" };
  return fetch(`/${dev}Basics/basics-main/${url}`, { headers })
}

if(type === 'page') {
  const options = { pageNum: params, pageSize: 10, eventTitle: search.value };
  if (publicParams?.value === '1') {
    options.occurrencePlace = instanceName
  } else if (publicParams?.value === '2') {
    options.eventType = instanceName
  } else if (publicParams?.value === '3') {
    options.eventGrade = instanceName
  } else {
    options.occurrencePlace = instanceName
  }
  queryManage.queryList(options, $event).then(({ success, result }) => {
    console.log({ success, result });
    if(success) {
      this.widgetObj.tree.pannelRerender(instanceName, { dataSource: result.result })
    }
  })
}else if(type === 'operation') {
  const { operation, data } = params;
  const eventParams = JSON.stringify({ eventId: data.id, ...data });

  // 周边资源变化
  this.pageObj.setVariables([{ name: 'event_record_id', value: data.id }]);
  this.getApiPage().setVariables([{ name: 'event_record_type', value: 1 }]);

  switch(operation.type) {
    case '处置': {
      window.parent.postMessage({ type: 'nav/style', pageName: '处置' }, document.referrer);
      var authTokenVar = this.getApiPage().getVariables().find(x => x.name == "AuthToken");
      this.getApiPage().navigateStage('833630884086153216', [{ name: 'screen_params', value: eventParams}, { name: 'AuthToken', value: authTokenVar.value }])
    }
      break;
    case '疏散':

      break;
    case '总结':
    {
      const timeline = this.getApiPage().getApiWidgetByAliasName('timelineVertical');
      const summary = this.getApiPage().getApiWidgetByAliasName('summary').widgetObj;
      timeline.hideWidget();
      this.pageObj.setVariables([{ name: 'timeline_event_id', value: data.id }]);
      summary.show();
      $('#summary .file-box').text(summary.fileName);

      window.parent.postMessage({ type: 'nav/style', pageName: '总结' }, document.referrer);
    }
      break;
    case '记录':
    {
      const timeline = this.getApiPage().getApiWidgetByAliasName('timelineVertical');
      const summary = this.getApiPage().getApiWidgetByAliasName('summary');
      summary.hideWidget();

      if (timeline && timeline.widgetObj) {
        const { setUpdateStatus, setVariable } = timeline.widgetObj;

        if (setUpdateStatus) setUpdateStatus(data.whetherProcess !== 1);
        if (setVariable) setVariable('timeline_event_id', data.id);
      }

      timeline.showWidget()
      window.parent.postMessage({ type: 'nav/style', pageName: '记录' }, document.referrer);

    }
      break;
    case '详情':
    {
      gis.widgetObj.setIconPosition({ right: 420, top: 100 });
      gis.widgetObj.setModalPosition({ right: 815, bottom: 20 });
      gis.widgetObj.sullivan.initial();
      const detail = this.getApiPage().getApiWidgetsByClassName('detail');
      const { eventDetails, areaId } = data;
      this.pageObj.setVariables([{ 'name': 'areaId', 'value': areaId }]);
      const setFormSelect = this.getApiPage().getApiWidgetByAliasName('judge-content').widgetObj;
      setFormSelect.areaId = areaId
      detail.forEach(v => v.showWidget())
      const $form = this.getApiPage().getApiWidgetByAliasName('judge-content');
      const $img = this.getApiPage().getApiWidgetByAliasName('detail-img');
      data.eventPoint = data.xCoordinate + '，' + data.yCoordinate + '，' + data.zCoordinate;
      const info = JSON.parse(eventDetails);
      const formData = {
        ...data,
        thumbnailPath: info.thumbnailPath,
        jpbPath: info.jpbPath,

      };
      // 详情时隐藏上传功能
      $form.widgetObj._option.config.forEach(v => {
        if (v.type == 'upload') {
          v.visible = true
        }
        if (v.dataIndex == 'sourceDeviceId') {
          v.visible = true
        }
      });

      try {
        $form.widgetObj._option.dataSource = formData;
        $form.widgetObj._option.isDetail = true;
        $form.widgetObj.render();
      } catch (e) {
        console.error(e)
      }

      const $playback = this.getApiPage().getApiWidgetByAliasName('playback');
      // const info = JSON.parse(eventDetails)
      if (Object.keys(info).length !== 0) {
        if (info.thumbnailPath) {
          const srcArr = info?.thumbnailPath.split(',').map(v => ({ src: v }));
          $img.widgetObj.setData([{ data: srcArr }]);
          $img.widgetObj.render()
        } else {
          $img.widgetObj.setData([{ data: [] }]);
          $img.widgetObj.render();
        }

        if (info?.jpbPath) {
          // 打开回放
          $playback.widgetObj.setData([{ data: info?.jpbPath }]);
          $playback.showWidget();
          $playback.widgetObj.render();
        } else {
          $playback.hideWidget();
          window.layer.msg('暂无录像回放')
        }
      } else {
        $img.widgetObj.setData([{ data: [] }]);
        $img.widgetObj.render();

        $playback.hideWidget();
        window.layer.msg('暂无录像回放')
      }

      if (data.sourceDeviceId) {
        // 打开实时视频
        basicsFetch(`/system/equipmentObject/listDetailByEquipmentCode?equipmentCode=${data.sourceDeviceId}`)
          .then(res => res.json())
          .then(json => {
            if (json?.success && json.result) {
              const { rows } = json.result;
              const data = rows[0]?.equipmentConfig?.gbVideoConfig;
              var host = this.getApiPage().getVariables().find(x => x.name == "stream_url").value;
              gis.widgetObj.sullivan.close();
              gis.widgetObj.sullivan.initial();
              if (data && data.sipUser && data.videoChannelId) {
                gis.widgetObj.sullivan.addPlayer(`${host}/api/v1/stream/rtc/play`, `${data.sipUser}/${data.videoChannelId}`)
              } else {
                window.layer.msg('暂无设备号/通道号')
              }
            }
          })
      } else {
        gis.widgetObj.sullivan.close();
        window.layer.msg('未关联摄像头');
      }
    }
      break;
    case '合并列表':
    {
      const eventTime = this.pageObj.getApiWidgetsByClassName('event-time');
      const merge_modals = this.pageObj.getApiWidgetsByClassName('event-merge');
      merge_modals.forEach(v => v.hideWidget());
      eventTime.forEach(v => v.showWidget());

      this.pageObj.setVariables([{ name: 'event_parentId', value: data.id }])
    }
      break;
    case '合并':
    {
      const merge_modals = this.pageObj.getApiWidgetsByClassName('event-merge');
      const eventTime = this.pageObj.getApiWidgetsByClassName('event-time');
      merge_modals.forEach(v => v.showWidget());
      eventTime.forEach(v => v.hideWidget());
    }
      break;
  }
}
